//
//  HomeViewController.m
//  KindergartenDemo
//
//  Created by 李传铎 on 2020/11/7.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "HomeViewController.h"
#import "SpellGroupManagementHomeViewController.h"

#import "HomeListTableViewCell.h"
#import "YHTableView.h"
@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource,HomeCellDelegate>
@property (nonatomic, strong) HomeListTableViewCell *containerCell;
@property (nonatomic , strong) YHTableView *tableView;

@property (nonatomic , strong) UIView *headerView;
@property (nonatomic , strong) UIView *sectionView;
@property (nonatomic , strong) UILabel *amount;
@property (nonatomic , strong) UILabel *orderCount;
@property (nonatomic , strong) UILabel *yesterdayOrderCountName;
@property (nonatomic , strong) UILabel *yesterdayAmountName;
@property (nonatomic, assign) BOOL canScroll;
@property (nonatomic, assign) NSInteger bntTag;

@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self creatUI];
}
- (void)creatUI{
    self.tableView = [[YHTableView alloc]initWithFrame:CGRectMake(0, __kNavigationBarHeight__, SCREEN_WIDTH, SCREEN_HEIGHT - __kNavigationBarHeight__ - 36) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(__kNavigationBarHeight__);
//        make.left.mas_equalTo(0);
//        make.right.mas_equalTo(0);
//        make.bottom.mas_equalTo(0);
//    }];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getMessage)];
//    [_tableView.mj_header beginRefreshing];
    [_tableView reloadData];
    UILabel *titleL = [[UILabel alloc]init];
    [self.view addSubview:titleL];
    titleL.text = @"EF英孚青少儿英语培训";
    titleL.textColor = colorCode(@"333333");
    titleL.font = [UIFont boldSystemFontOfSize:22];
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"LCDleaveTops" object:nil];
    UIImageView *mainImageV = [[UIImageView alloc]init];
    [self.view addSubview:mainImageV];
    mainImageV.image = imageNameStr(@"pic_jigou_tips");
    
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(__kNavigationBarHeight__ - 34);
        make.left.mas_equalTo(15);
        
    }];
    
    [mainImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(titleL.mas_centerY);
        make.left.mas_equalTo(titleL.mas_right).offset(20);
        make.width.mas_equalTo(51);
        make.height.mas_equalTo(20);
    }];
    
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 235;
    }
    return 46;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 235)];
        [view addSubview:self.headerView];
        return view;
    }else{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 46)];
        [view addSubview:self.sectionView];
        return view;
    }
}
- (void)pickTitleClick:(UIButton *)sender{
    self.bntTag = sender.tag - 2000;
    for (int i = 0; i < 5; i ++) {
        UIButton *button = [_sectionView viewWithTag:2000 + i];
        button.selected = NO;
        button.titleLabel.font = fontCount(14);
    }
    
    sender.selected = YES;
    sender.titleLabel.font = fontCount(16);
    self.containerCell.isSelectIndex = YES;
    [self.containerCell.scrollView setContentOffset:CGPointMake((sender.tag - 2000)*[UIScreen mainScreen].bounds.size.width, 0) animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.frame.size.height - __kNavigationBarHeight__ - 46 - __kTabBarHeight__ ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        // 重点！横向滑动cell
         HomeListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"containerCell"];
         
         if (!cell) {
             cell = [[HomeListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"containerCell"];
         }
        self.containerCell = cell;
        cell.delegate = self;
         
         return cell;
    }
    return nil;
}
#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
        NSInteger count = 0;
        CGFloat bottomCellOffset = [self.tableView rectForSection:count].origin.y;
        bottomCellOffset = floorf(bottomCellOffset);
        
        if (scrollView.contentOffset.y >= bottomCellOffset + 235) {
            scrollView.contentOffset = CGPointMake(0, bottomCellOffset + 235);
            if (self.canScroll) {
                self.canScroll = NO;
                self.containerCell.objectCanScroll = YES;
                
            }
        }else{
            //子视图没到顶部
            if (!self.canScroll) {
                
                scrollView.contentOffset = CGPointMake(0, 235 + bottomCellOffset);
            }
        }
    }
}
#pragma mark - YHDContainerCellDelegate

- (void)mmtdOptionalScrollViewDidScroll:(UIScrollView *)scrollView {
    self.tableView.scrollEnabled = NO;
}

- (void)mmtdOptionalScrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger index = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
    //    [self.sectionView.segmentControl setSelectedSegmentIndex:page animated:YES];
//    for (int i = 0; i < 5; i ++) {
//        UIButton *button = [_sectionView viewWithTag:1000 + i];
//        if (i == index) {
//            button.selected = YES;
//        }else{
//            button.selected = NO;
//        }
//    }
    self.tableView.scrollEnabled = YES;
}
#pragma mark - Notification

- (void)changeScrollStatus {
    self.canScroll = YES;
    self.containerCell.objectCanScroll = NO;
}
- (void)hySegmentedControlSelectAtIndex:(NSInteger)index{
    self.containerCell.isSelectIndex = YES;
    [self.containerCell.scrollView setContentOffset:CGPointMake(index*[UIScreen mainScreen].bounds.size.width, 0) animated:YES];
}
//@"拼团管理",@"服务管理",@"分组管理",@"推广监控" 点击方法
- (void)nextClick:(UIButton *)sender{
    SpellGroupManagementHomeViewController *vc = [[SpellGroupManagementHomeViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)orderNextClick{
    
}
- (UIView *)headerView{
    if(!_headerView){
        //头试图
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 235)];
        [self.view addSubview:_headerView];
        _headerView.backgroundColor = [UIColor whiteColor];
        UIView *orderBgView = [[UIView alloc]init];
        [_headerView addSubview:orderBgView];
        [orderBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(16);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(100);
        }];
        UIImageView *orderImageV = [[UIImageView alloc]init];
        [orderBgView addSubview:orderImageV];
        orderImageV.image = imageNameStr(@"pic_home_bg_blue");
        [orderImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
        
        UILabel *amountName = [[UILabel alloc]init];
        [orderBgView addSubview:amountName];
        amountName.text = @"今日成交额/元";
        amountName.font = fontCount(12);
        amountName.textColor = [UIColor whiteColor];
        
        [amountName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.top.mas_equalTo(12);
            
        }];
        
        _amount = [[UILabel alloc]init];
        [orderBgView addSubview:_amount];
        _amount.text = @"12,345,78.00";
        _amount.font = [UIFont boldSystemFontOfSize:22];
        _amount.textColor = [UIColor whiteColor];
        
        [_amount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.top.mas_equalTo(amountName.mas_bottom).offset(10);
            
        }];
        
        _yesterdayAmountName = [[UILabel alloc]init];
        [orderBgView addSubview:_yesterdayAmountName];
        _yesterdayAmountName.text = @"昨日成交额:12,345,678.00";
        _yesterdayAmountName.font = fontCount(12);
        _yesterdayAmountName.textColor = [UIColor whiteColor];
        
        [_yesterdayAmountName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(12);
            make.top.mas_equalTo(_amount.mas_bottom).offset(10);
            
        }];
        
        UIButton *orderNextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [orderBgView addSubview:orderNextBtn];
        [orderNextBtn addTarget:self action:@selector(orderNextClick) forControlEvents:UIControlEventTouchUpInside];
        [orderNextBtn setImage:imageNameStr(@"ic_more_small") forState:0];
        [orderNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-12);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(15);
        }];
        
        UILabel *orderCountName = [[UILabel alloc]init];
        [orderBgView addSubview:orderCountName];
        orderCountName.text = @"今日成交额/元";
        orderCountName.font = fontCount(12);
        orderCountName.textColor = [UIColor whiteColor];
        
        [orderCountName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(amountName.mas_right).offset(90);
            make.top.mas_equalTo(12);
            
        }];
        
        _orderCount = [[UILabel alloc]init];
        [orderBgView addSubview:_orderCount];
        _orderCount.text = @"12,345,78.00";
        _orderCount.font = [UIFont boldSystemFontOfSize:22];
        _orderCount.textColor = [UIColor whiteColor];
        
        [_orderCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(orderCountName);
            make.top.mas_equalTo(orderCountName.mas_bottom).offset(10);
            
        }];
        
        _yesterdayOrderCountName = [[UILabel alloc]init];
        [orderBgView addSubview:_yesterdayOrderCountName];
        _yesterdayOrderCountName.text = @"昨日成交额:12,345,678.00";
        _yesterdayOrderCountName.font = fontCount(12);
        _yesterdayOrderCountName.textColor = [UIColor whiteColor];
        
        [_yesterdayOrderCountName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(orderCountName);
            make.top.mas_equalTo(_orderCount.mas_bottom).offset(10);
            
        }];
        
        
        UIImageView *marketImageV = [[UIImageView alloc]init];
        [_headerView addSubview:marketImageV];
        marketImageV.image = imageNameStr(@"ic_home_tips_red");
        
        [marketImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(orderBgView.mas_bottom).offset(5);
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(20);
        }];
        
        
        
        UILabel *marketL = [[UILabel alloc]init];
        [_headerView addSubview:marketL];
        marketL.font = fontCount(13);
        marketL.textColor = colorCode(@"#323A5A");
        marketL.text = @"商家端即将升级分享功能，敬请留意";
        
        [marketL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(marketImageV.mas_right).offset(8);
            make.centerY.mas_equalTo(marketImageV.mas_centerY);
        }];
        UIView *diView = [[UIView alloc]init];
        [_headerView addSubview:diView];
        
        [diView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(marketImageV.mas_bottom).offset(10);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(80);
        }];
        NSArray *nameArr = @[@"拼团管理",@"服务管理",@"分组管理",@"推广监控"];
        NSArray *imageArr = @[@"ic_home_pintuan",@"ic_home_fuwu",@"ic_home_fenzu",@"ic_home_guanlis"];
        for (int i = 0; i < nameArr.count; i++) {
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(15 + (SCREEN_WIDTH - 30)/nameArr.count *i, 0, (SCREEN_WIDTH - 30)/nameArr.count, 80)];
            [diView addSubview:view];
            
            UIImageView *imageV = [[UIImageView alloc]init];
            imageV.image = imageNameStr(imageArr[i]);
            [view addSubview:imageV];
            
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(5);
                make.centerX.mas_equalTo(view.mas_centerX);
                make.height.mas_equalTo(30);
                make.width.mas_equalTo(30);
            }];
            UILabel *label = [[UILabel alloc]init];
            label.font = fontCount(13);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = colorCode(@"#323A5A");
            label.text = nameArr[i];
            [view addSubview:label];
            
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(imageV.mas_bottom).offset(10);
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
            }];
            UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [view addSubview:nextBtn];
            nextBtn.tag = 1000 + i;
            [nextBtn addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
            [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.and.bottom.mas_equalTo(0);
            }];
        }
    }
    return _headerView;
}
-(UIView *)sectionView{
    if (!_sectionView) {
        _sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 46)];
        _sectionView.backgroundColor = [UIColor whiteColor];
        UIButton * orderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [orderBtn addTarget:self action:@selector(pickTitleClick:) forControlEvents:UIControlEventTouchUpInside];
        [orderBtn setTitleColor:colorCode(@"#84899D") forState:0];
        [orderBtn setTitle:@"待处理订单" forState:0];
        [orderBtn setTitleColor:colorCode(@"##F34B4D") forState:UIControlStateSelected];
        [_sectionView addSubview:orderBtn];
        [orderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(15);
            make.width.mas_equalTo(90);
            make.bottom.mas_equalTo(0);
        }];
        orderBtn.selected = YES;
        orderBtn.titleLabel.font = fontCount(16);
        UIButton * InformationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [InformationBtn addTarget:self action:@selector(pickTitleClick:) forControlEvents:UIControlEventTouchUpInside];
        [_sectionView addSubview:InformationBtn];
        [InformationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(orderBtn.mas_right).offset(10);
            make.width.mas_equalTo(75);
            make.bottom.mas_equalTo(0);
        }];
        [InformationBtn setTitle:@"资讯分享" forState:0];
        [InformationBtn setTitleColor:colorCode(@"#84899D") forState:0];
        [InformationBtn setTitleColor:colorCode(@"#F34B4D") forState:UIControlStateSelected];
        InformationBtn.titleLabel.font = fontCount(14);
        orderBtn.tag = 2000;
        InformationBtn.tag = 2001;
    }
    return _sectionView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
